from django.db import models
from django.utils import timezone
from .wechat_user import WechatUser

class Audio(models.Model):
    """音频模型"""
    STATUS_CHOICES = (
        ('pending', '等待处理'),
        ('processing', '处理中'),
        ('completed', '已完成'),
        ('failed', '处理失败'),
    )
    
    title = models.CharField(max_length=255, verbose_name='标题', blank=True, null=True)
    text = models.TextField(verbose_name='文本内容')
    voice = models.CharField(max_length=100, verbose_name='语音')
    rate = models.CharField(max_length=20, verbose_name='语速')
    volume = models.CharField(max_length=20, verbose_name='音量', blank=True, null=True)
    style = models.CharField(max_length=50, verbose_name='情感风格', blank=True, null=True)
    style_degree = models.FloatField(verbose_name='情感强度', default=1.0)
    file_name = models.CharField(max_length=255, verbose_name='文件名')
    url = models.URLField(verbose_name='文件URL', blank=True, null=True)
    status = models.CharField(max_length=20, verbose_name='状态', choices=STATUS_CHOICES, default='pending')
    created_at = models.DateTimeField(default=timezone.now, verbose_name='创建时间')
    user = models.ForeignKey(WechatUser, on_delete=models.CASCADE, verbose_name='用户', null=True, blank=True)

    class Meta:
        db_table = 'audio'
        verbose_name = '音频'
        verbose_name_plural = verbose_name
        ordering = ['-created_at']

    def __str__(self):
        return f"{self.title or '未命名'} - {self.created_at.strftime('%Y-%m-%d %H:%M:%S')}"

class AudioUsage(models.Model):
    """用户语音合成使用记录模型"""
    user = models.ForeignKey(WechatUser, on_delete=models.CASCADE, verbose_name='用户')
    date = models.DateField(default=timezone.now, verbose_name='使用日期')
    total_chars = models.IntegerField(default=0, verbose_name='总字符数')
    total_count = models.IntegerField(default=0, verbose_name='总次数')
    created_at = models.DateTimeField(default=timezone.now, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        db_table = 'audio_usage'
        verbose_name = '语音合成使用记录'
        verbose_name_plural = verbose_name
        ordering = ['-date']
        unique_together = ['user', 'date']  # 每个用户每天只有一条记录

    def __str__(self):
        return f"{self.user.nickname} - {self.date.strftime('%Y-%m-%d')} - {self.total_count}次" 